package com.sparrow.redis;
import com.sparrow.common.util.PropertiesUtil;
import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.redisson.codec.JsonJacksonCodec;
import org.redisson.config.Config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

@Configuration
@ComponentScan("com.sparrow.redis")
public class SparrowRedisAutoConfig {

    @Bean(destroyMethod = "shutdown")
    public RedissonClient redissonClient() {
        Config config = new Config();
        //使用json序列化
        config.setCodec(new JsonJacksonCodec());
        //redis://127.0.0.1:6379
        String address = PropertiesUtil.getProperty("redis.address");
        String password = PropertiesUtil.getProperty("redis.password");
        config.useSingleServer()
                .setAddress(address)
                .setDatabase(0)
                .setPassword(password); // 如果有密码就写上
        return Redisson.create(config);
    }
}

